import java.util.Arrays;

public class Main {
    public static int solution(int m, String s) {
        int n = s.length();
        boolean[] vis = new boolean[n];
        Arrays.fill(vis, false);
        int ret = 0;
        int pos = 0;

        // 初步匹配 "UCC" 子串
        while (m >= 0 && pos + 2 < n) {
            if (s.charAt(pos) == 'U' && s.charAt(pos + 1) == 'C' && s.charAt(pos + 2) == 'C') {
                ret++;
                vis[pos] = vis[pos + 1] = vis[pos + 2] = true;
                pos += 3;
            } else {
                pos++;
            }
        }

        // 尝试通过替换操作形成 "UCC"
        pos = 0;
        while (m > 0 && pos + 1 < n) {
            if (vis[pos] || vis[pos + 1]) {
                pos++;
                continue;
            }
            if (s.charAt(pos) == 'U' && s.charAt(pos + 1) == 'C') {
                ret++;
                m--;
                vis[pos] = vis[pos + 1] = true;
                pos += 2;
            } else {
                pos++;
            }
        }

        // 尝试通过替换操作形成 "CC"
        pos = n - 1;
        while (m > 0 && pos - 1 >= 0) {
            if (vis[pos] || vis[pos - 1]) {
                pos--;
                continue;
            }
            if (s.charAt(pos) == 'C' && s.charAt(pos - 1) == 'C') {
                ret++;
                m--;
                vis[pos] = vis[pos - 1] = true;
                pos -= 2;
            } else {
                pos--;
            }
        }

        // 处理剩余的编辑距离
        pos = 0;
        while (m >= 2 && pos < n) {
            if (vis[pos]) {
                pos++;
            } else {
                m -= 2;
                ret++;
                pos++;
            }
        }

        // 计算剩余编辑距离可以形成的 "UCC" 子串
        ret += m / 3;
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(solution(3, "UCUUCCCCC") == 3);
        System.out.println(solution(6, "U") == 2);
        System.out.println(solution(2, "UCCUUU") == 2);
    }
}

